﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace Fayshine.VA.Helpers
{
    public static class ExtentionMethods
    {
        public static void each<T>(this IEnumerable<T> items, Action<T> action)
        {
            foreach (T item in items)
            {
                action(item);
            }
        }

        public static void HandlePropertyChanges(
            this FrameworkElement element, string propertyName,
            PropertyChangedCallback callback)
        {
            //Bind to a depedency property 
            Binding b = new Binding(propertyName) { Source = element };
            var prop = System.Windows.DependencyProperty.RegisterAttached(
                "ListenAttached" + propertyName,
                typeof(object),
                typeof(UserControl),
                new System.Windows.PropertyMetadata(callback));

            element.SetBinding(prop, b);
        }
        public static void MakeAutoDismissing(this Popup popup, UIElement shield)
        {

            // Whenever the popup opens, deploy the shield 
            popup.HandlePropertyChanges(
                "IsOpen",
                (s, e) =>
                {
                    shield.Visibility = (bool)e.NewValue
                        ? Visibility.Visible : Visibility.Collapsed;
                }
            );

            // Whenever the shield is clicked, dismiss the popup 
            shield.MouseLeftButtonDown += (s, e) => popup.IsOpen = false;
        }

    }
    public static class PopupUtils
    {

    }

    public static class FrameworkUtils
    {

    }

}
